package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.aop.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.enumeration.OperationType;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@Builder
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
    /**
     * 根据菜品分类查询菜品信息
     * @param categoryId 菜品分类ID
     * @return 菜品信息
     */
    @Override
    public List<Dish> getByIdDish(Long categoryId) {
        //只能添加起售的菜品
        Dish dish= Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        List<Dish> dishList = dishMapper.getByCategoryIdDish(dish);
       // log.info("[dishList] dishList:{}",dishList);
        return dishList;
    }
    /**
     * 新增菜品
     * @param dishDTO
     */
    @AutoFill(OperationType.INSERT)
    public void addBatch(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        dishMapper.insert(dish);

        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }

            dishFlavorMapper.insertBatch(flavors);

        }
    }

    /**
     * 分页查询
     * @param dto  条 和 页码
     * @return 查询后的数据
     */

    public PageResult byPageDish(DishPageQueryDTO dto) {
        //使用插件 完成分页查询
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        // 调用mapper 执行SQL语句并返回数据
        Page<DishVO> page= (Page<DishVO>)dishMapper.byPageDish(dto);
        //数据 返回给控制器             总数                菜品数据
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除
     * @param ids ID值
     */
    @Transactional(rollbackFor = Exception.class)  //多表操作 使用事务注解
    public void deleteDish(List<Long> ids) {
        //判断状态 是否在启用
       List<Integer> status = dishMapper.seleteStatus(ids);
        for (Integer statu : status) {
            if (statu == StatusConstant.ENABLE ) throw  new PasswordErrorException(MessageConstant.DISH_ON_SALE);
        }

        //被关联的菜品不能被删除
       Long dish = dishFlavorMapper.selectDish(ids);
        if (dish == null || dish > 0) throw new PasswordErrorException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        //删除菜品后关联的口味数据也需要删除
        dishMapper.deleteByidDish(ids);
        //基于ID 删除口味
        dishMapper.deleteById(ids);
    }

    /**
     * 基于ID查询菜品信息
     * @param id 菜品ID
     * @return 菜品信息
     */
    @Transactional  //多表事务
    public DishVO getDishVO(Long id) {
        // 通过ID 回显菜品信息
       Dish dish = dishMapper.getDish(id);
        //通过ID 回显 口味信息
        List<DishFlavor> dishFlavorList = dishFlavorMapper.getDish(id);
        //把所有信息添加到一个对象中
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    /**
     *根据信息 更新数据库
     * @param dishDTO 菜品信息
     */
    @Transactional  //多表事务
    public void updateDishById(DishDTO dishDTO) {
        //修改菜品信息
       //
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.upadateByidDish(dish);
        //修改口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {   //获得菜品ID
            flavor.setDishId(dishDTO.getId());
        }
        dishFlavorMapper.deleteByIdFlavor(dish.getId());  //先删除在更新
        dishFlavorMapper.insertBatch(flavors);  //更新
    }


}
